package com.squirrel.michale;

import java.util.*;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/17 7:16 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0207 {

    // https://www.bilibili.com/video/BV1xt4y1D79h/?spm_id_from=333.337.search-card.all.click&vd_source=dd94464bac13f791ea6a2e385d28295a

    public boolean canFinish(int numCourses, int[][] prerequisites) {

        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < numCourses; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < prerequisites.length; i++) {
            graph.get(prerequisites[i][1]).add(prerequisites[i][0]);
        }
        int[] visited = new int[numCourses];
        for (int i = 0; i < numCourses; i++) {
            if (findCircle(i, visited, graph)) {
                return false;
            }
        }
        return true;

    }


    public boolean findCircle(int node, int[] visited, List<List<Integer>> graph) {
        if (visited[node] == 1) {
            return true;
        }

        if (visited[node] == 2) {
            return false;
        }
        visited[node] = 1;
        for (Integer next : graph.get(node)) {
            if (findCircle(next, visited, graph)) {
                return true;
            }
        }
        visited[node] = 2;

        return false;
    }

    public boolean canFinish2(int numCourses, int[][] prerequisites) {

//        int[] result = new int[];
//
//        Map<Integer, List<Integer>> m = new HashMap<>();
//        Map<Integer, Integer> inDegree = new HashMap<>();
//
//        for (int[] pre : prerequisites) {
//            int curCourse = pre[0];
//            int depCourse = pre[1];
//
//            if (m.get(depCourse) == null) {
//
//                List<Integer> depCourses = new ArrayList<>();
//                depCourses.add(depCourse);
//                m.put(curCourse, depCourses);
//            }else {
//                m.get(curCourse).add(depCourse);
//            }
//
//            inDegree.put(curCourse, inDegree.get(curCourse) + 1);
//
//
//        }
//
//        Deque<Integer> deque = new ArrayDeque<>();
//
//        for (int i = 0; i < numCourses; i++) {
//            if (inDegree.get(i) == 0) {
//
//                deque.offer(inDegree.get(i));
//            }
//        }
//
//        while (!deque.isEmpty()) {
//            int curCourse = deque.poll();
//        }

        return true;
    }
}
